from bs4 import BeautifulSoup
import execjs,os,requests
import m3u8
requests.packages.urllib3.disable_warnings()
import ssl
import time
# ssl._create_default_https_context = ssl._create_stdlib_context
import urllib.request
from urllib.request import urlopen
import ssl
import json
ssl._create_default_https_context = ssl._create_unverified_context


import httplib2
httplib2.HTTPConnectionWithTimeout._http_vsn=10
httplib2.HTTPConnectionWithTimeout._http_vsn_str = 'HTTP/1.0'


import sys
print(sys.getrecursionlimit())
sys.setrecursionlimit(65530)





def get_headers():
    # with open("1.js", 'r', encoding='utf-8') as f:
    #     data = f.read()
    # os.environ["EXECJS_RUNTIME"] = "Node"
    # print(execjs.get().name)
    # # 执行js脚本，获取返回值 url
    # context1 = execjs.compile(data)  # Node.js (V8)
    # user_uuid = context1.call("generateUUID2")
    # print(user_uuid)

    headers = {
        # "Cookie": "user_uuid=" + str(user_uuid),
        # "Accept":"*/*",
        # "Accept-Encoding":"gzip, deflate, br, zstd",
        # "Accept-Language":"zh-CN,zh;q=0.9",
        "Dnt": "1",
        "Referer":"https://www.fofoyy.com",
        "Priority":"u=1, i",
        "Sec-Ch-Ua": '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
        "Sec-Ch-Ua-Mobile": "?0",
        "Sec-Ch-Ua-Platform": "Windows",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "cross-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",

    }
    return headers

headers = get_headers()
def get_page(url):

    res = requests.get(url=url, headers=headers,verify=False)

    print(res.status_code)
    # print(res.text)
    return  res.text


def get_m3u8(data):
    soup = BeautifulSoup(data)

    # print(soup.prettify())
    scripts = soup.find_all('script')
    s = scripts[7]
    data = str(s.contents[0])[str(s.contents[0]).index("eval"):str(s.contents[0]).index("const video")]
    print(data[5:-2])
    import execjs, os, requests
    #
    # with open("1.js", 'r', encoding='utf-8') as f:
    #     data = f.read()
    os.environ["EXECJS_RUNTIME"] = "Node"
    print(execjs.get().name)
    # 执行js脚本，获取返回值 url
    m3u8_url = execjs.eval(data)  # Node.js (V8)

    print(m3u8_url)

    return m3u8_url
PATH=r"E:\1\BaiduSyncdisk"
def down_mp4(m3u8_url):
    # https://surrit.com/de395aa4-dd5f-43e6-820c-54c42c36b8d4/720p/video.m3u8
    # res = requests.get(m3u8_url)
    # print(res.text)
    obj = m3u8.load(m3u8_url,verify_ssl=False)  # this could also be an absolute filename
    # print(obj.segments)
    # print(playlist.target_duration)
    # print(playlist.dumps())
    url_list=[]
    m3u8_url_t = m3u8_url[:m3u8_url.index("video")]
    for s in obj.segments:
        if "video" in str(s):
            u_tmp = str(s).split(":")[1].split(",")[1].strip()
            # print(m3u8_url_t+u_tmp)
            url_list.append(m3u8_url_t+u_tmp)
    count = 0
    filename=m3u8_url_t.split("/")[3]+".webm"
    filename = os.path.join(PATH, filename)
    print("filename-->", filename)

    with open(filename,'wb') as f:

        for u in url_list:
            try:
                print("url---",u)
                res = requests.get(u, headers=headers, verify=False)
                print(f'总共{len(url_list)}个段视频，正在下载第{count}个"')
                count += 1
                f.write(res.content)
            except:
                time.sleep(10)
                res = requests.get(u, headers=headers, verify=False)
                print(f'总共{len(url_list)}个段视频，正在下载第{count}个"')
                count += 1
                f.write(res.content)
    """
    #EXTINF:4,
video2248.jpeg
    """



# url = "https://missav.com/dm26/cn/sone-133-chinese-subtitle"
# url = "https://missav.com/dm52/cn/ssis-433-uncensored-leak"
# url = "https://missav.com/dm39/cn/ssis-492-uncensored-leak"
# https://missav.com/dm52/cn/ssis-378-uncensored-leak
url = 'https://missav.com/dm31/cn/ipzz-139-uncensored-leak#02aeaf20-97c8-45f3-8d46-f5c8f11a71dd_desktop-home-segment-items'
# https://surrit.com/cf8744fe-4c74-4cad-830e-7a45dfee0d38/1280x720/video.m3u8
# data = get_page(url)
# m3u8_url = get_m3u8(data)
# down_mp4(m3u8_url)
# down_mp4("https://surrit.com/de395aa4-dd5f-43e6-820c-54c42c36b8d4/720p/video.m3u8")

# with open("m3u8.html",'r',encoding='utf8') as f:
#     data = f.read()

# m3u8_url = get_m3u8(data)
# print(m3u8_url) https://surrit.com/7dab4666-5249-4949-9709-312195fd9e1d/1280x720/video.m3u8

session = requests.Session()

def download_m3u8_video(url,file_path):
    # 通过请求xx.m3u8文件，获取视频ts的url,下载ts文件到一个文件中
    r = session.get(url,headers=headers, verify=False)
    if r.status_code != 200:
        print('m3u8视频下载链接无效')
        return False

    m3u8_list = r.text.split('\n')
    # m3u8_url_t = url[:url.index("video")]
    # m3u8_url_t = url.split('/')[:-1]
    # m3u8_url_t =  "".join(m3u8_url_t)
    # print("m3u8_url_t",m3u8_url_t)
    i = url.rindex('/')
    m3u8_url_t = url[:i]
    print("m3u8_url_t", m3u8_url_t) #  https://v.cdnlz16.com/20230911/38410_55a88c1c/2000k/hls
    m3u8_list = [ m3u8_url_t+"/"+i for i in m3u8_list if i and i[0] != '#']

    print(m3u8_list)
    break_url = 0
    while True:
        try:
            with open(file_path, 'ab') as f:
                for ts_url in m3u8_list[break_url:]:
                    print("正在下载：" + ts_url)
                    break_url = m3u8_list.index(ts_url)
                    print("break_url", break_url)
                    with session.get(ts_url, headers=headers, verify=False,stream=True) as r:
                        print("status_code",r.status_code)
                        if r.status_code == 200:
                            # print(r.text)
                            f.write(r.content)
                            # raise Exception

        except Exception as e:
            # time.sleep(10)
            print(e)
            continue
        break
    print('m3u8视频下载完成')
    return True


# download_m3u8_video(m3u8_url) #['video0.jpeg', 'video1.jpeg', 'video2.jpeg', 'video3.jpeg'
# 'https://surrit.com/7dab4666-5249-4949-9709-312195fd9e1d/1280x720/video0.jpeg',

def convert_ts_to_mp4(ts_file_path, mp4_file_path):
    # 将ts文件转为mp4文件
    os.system(f'ffmpeg -i {ts_file_path} -c copy {mp4_file_path}')

def get_filename(m3u8_url):
    return  m3u8_url.split('/')[-3]

def download_all():
    # from missav.proxyIPS import get_proxy
    from missav.database import SqliteClass

    # max=1
    sql = SqliteClass("../missav/missav")
    sql.create_table("video",
                     "(name varchar(100) unique,url varchar(200),image_url varchar(100),video_type varchar(100) )")
    sql.create_table("video_detail",
                     "(name varchar(100),url varchar(200),video_url varchar(100),video_name varchar(100))")

    values = sql.select_all()

    sql.cursor.execute("select * from video_detail;")
    values = sql.cursor.fetchall()
    for v in values:
        # proxies = get_proxy()
        m3u8_url = v[2]
        mp4_filename_ori = v[-1]
        print("m3u8_url:",m3u8_url+"\tmp4_filename_ori"+mp4_filename_ori)
        import os

        import platform

        if platform.system() == "Windows":  # Windows Linux
            ts_file_path = r'E:\1\BaiduSyncdisk'
            mp4_file_path = r'E:\1\BaiduSyncdisk'
        else:
            ts_file_path = r'/video'
            mp4_file_path = r'/video'

        if mp4_filename_ori in os.listdir(ts_file_path):
            continue
        # 获取生成的ts文件的完整路径
        # print("get_filename",get_filename(m3u8_url))
        # filename = get_filename(m3u8_url)
        filename = mp4_filename_ori.split('.')[0]
        ts_filename = filename + ".ts"
        mp4_filename = filename + ".mp4"
        ts_file_path = os.path.join(ts_file_path, ts_filename)
        print("ts_file_path", ts_file_path)
        mp4_file_path = os.path.join(mp4_file_path, mp4_filename)
        print("mp4_file_path", mp4_file_path)
        download_m3u8_video(m3u8_url, ts_file_path)
        convert_ts_to_mp4(ts_file_path, mp4_file_path)
if __name__ == '__main__':
    # while True:
    #     try:
    #         download_all()
    #     except Exception as e:
    #         print("main",e)
    #         continue
    #     break


    url = "https://v.cdnlz16.com/20230911/38410_55a88c1c/2000k/hls/mixed.m3u8"

    import os

    import platform

    filename = "1"
    if platform.system() == "Windows":  # Windows Linux
        ts_file_path = r'E:\1\BaiduSyncdisk'
        mp4_file_path = r'E:\1\BaiduSyncdisk'
    else:
        ts_file_path = r'/video'
        mp4_file_path = r'/video'
    ts_filename = filename + ".ts"
    mp4_filename = filename + ".mp4"
    ts_file_path = os.path.join(ts_file_path, ts_filename)
    print("ts_file_path", ts_file_path)
    mp4_file_path = os.path.join(mp4_file_path, mp4_filename)

    download_m3u8_video(url,ts_file_path)
    convert_ts_to_mp4(ts_file_path, mp4_file_path)

